/*
 * @author Javier A. Ramírez M.
 * jaramirez85@yahoo.es
 */

package com.jav;

import com.jav.bean.Pedido;
import com.jav.bean.Productos;
import com.jav.util.TickerRenderer;
import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Graphics;
import com.sun.lwuit.List;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.list.DefaultListCellRenderer;
import com.sun.lwuit.plaf.Style;
import com.sun.lwuit.plaf.UIManager;


/**
 *
 * @author Javier
 */
public class PedidoDialog extends Dialog{
    
    private List prods;


    public PedidoDialog(Pedido ped) {
        super("CONSULTA");
        setLayout(new BorderLayout());
        prods =  new List(ped.getVp());
       /* prods =  new List(ped.getVp()){
            private long tickTime = System.currentTimeMillis();
            private int lastSelection = -1;

            public boolean animate() {
                boolean val = super.animate();
                if(hasFocus()) {
                    long currentTime = System.currentTimeMillis();
                    if(currentTime - tickTime > 400) {
                        if(lastSelection == getSelectedIndex()) {
                            renderer.incrementPosition();
                        }
                        else {
                            lastSelection = getSelectedIndex();
                            renderer.resetPosition();
                        }
                        tickTime = currentTime;
                        return true;
                    }
                }
                return val;
            }

        };*/

        prods.setListCellRenderer(new TickerRenderer());

        prods.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ev) {
                Object obj = ev.getSource();
                Productos p = (Productos) ((List)obj).getSelectedItem();
                //si el producto no es nulo
                if (p != null){
                    //y su estado eliminar es false
                    if ( !p.isEliminar()){
                        if(Dialog.show("¿?", "¿ELIMINAR ESTE PRODUCTO?", "SI", "NO")){
                            p.setEliminar(true);
                        }
                    }
                    //
                    else{
                        if(Dialog.show("¿?", "ESTE PRODUCTO YA ESTÁ ELIMANADO. ¿DESEA REVERSAR SU ESTADO?", "SI", "NO")){
                            p.setEliminar(false);
                        }
                    }
                }

                
                


            }
        });
        addComponent(BorderLayout.CENTER,prods);
        addCommand(new Command("OK"));

    }



}


/*class TickerRenderer extends DefaultListCellRenderer {
    private int position;

    public TickerRenderer() {
        super(false);
    }

    public void resetPosition() {
        position = 0;
    }

    public void incrementPosition() {
        position += 5;
    }

    public void paint(Graphics g) {
        if(hasFocus()) {
            String text = getText();
            Style s = getStyle();
            int width = s.getFont().stringWidth(text);
            if(getWidth() >= width) {
                super.paint(g);
                return;
            }
            UIManager.getInstance().getLookAndFeel().setFG(g, this);
            int actualPosition = position % width;
            g.translate(-actualPosition, 0);
            g.drawString(text, getX() + s.getPadding(LEFT), getY());
            g.translate(width + 20, 0);
            g.drawString(text, getX(), getY());
            g.translate(actualPosition - 20 - width , 0);
        }
        else {
            super.paint(g);
        }
   }

    public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected) {
        Productos p = (Productos)value;
        String texto = "ELIMINADO";
        if (p!= null)
            texto = "[P:"+getFormat(p.getCantidad())+" C:"+getFormat(p.getCambio())+"]"+p.getProducto().getDes();

        super.getListCellRendererComponent(list, texto, index, isSelected);
        return this;
    }
    protected String getFormat (long valor){
        return String.valueOf(valor);
    }
}*/

